﻿using System.Collections.Generic;
using UnityEngine;

public class ModAction : IModBase
{
    /// <summary>
    /// 当前动作
    /// </summary>
    private IAction currentAction = null;

    /// <summary>
    /// 下一个动作
    /// </summary>
    private IAction nextAction = null;

    public ModAction(OwnerObj ownerObj) : base(ownerObj)
    {

    }

    /// <summary>
    /// 装载action
    /// </summary>
    /// <param name="nextAction"></param>
    public void PutOnAction(IAction nextAction)
    {
        this.nextAction = nextAction;
    }

    /// <summary>
    /// 停止
    /// </summary>
    public void Stop()
    {
        if (GetModule().Get<ModAttr>().IsDead())
        {
            return;
        }

        if (null != currentAction)
        {
            if (currentAction.IsCanInterrupt(null))
            {
                currentAction = null;
            }
        }

        nextAction = null;
    }

    /// <summary>
    /// 强制停止
    /// </summary>
    public void ForceStop()
    {
        if (GetModule().Get<ModAttr>().IsDead())
        {
            return;
        }

        currentAction = null;
        nextAction = null;
    }

    /// <summary>
    /// 移动
    /// </summary>
    /// <param name="targetPos"></param>
    public void Move(Vector3 targetPos)
    {
        if (GetModule().Get<ModAttr>().IsDead())
        {
            return;
        }

        List<Vector3> path = new List<Vector3>
        {
            targetPos
        };

        IMoveAction moveAction = new MoveAction(ownerObj);
        moveAction.SetPath(path);
        moveAction.Start();
    }

    public void Move(List<Vector3> path)
    {
        if (GetModule().Get<ModAttr>().IsDead())
        {
            return;
        }

        IMoveAction moveAction = new MultiMoveActon(ownerObj);
        moveAction.SetPath(path);
        moveAction.Start();
    }

    /// <summary>
    /// 释放技能
    /// </summary>
    /// <param name="skillId"></param>
    /// <param name="targetId"></param>
    public void Attack(int targetId)
    {
        if (GetModule().Get<ModAttr>().IsDead())
        {
            return;
        }

        ISkillAction skillAction = new CommonSkillAction(ownerObj);
        skillAction.SetTarget(targetId);
        skillAction.Start();
    }

    public void Die()
    {
        DieAction dieAction = new DieAction(ownerObj);
        dieAction.Start();
    }

    /// <summary>
    /// 更新
    /// </summary>
    /// <param name="deltaTime"></param>
    public override void Update(float deltaTime)
    {
        if (null == currentAction)
        {
            if (null != nextAction)
            {
                currentAction = nextAction;
                nextAction = null;

                currentAction.Load();
            }
        }
        else
        {
            if (null != nextAction)
            {
                // 下一个动作中断当前动作
                if (currentAction.IsCanInterrupt(nextAction))
                {
                    currentAction.Interrupt();
                    currentAction.Unload();

                    currentAction = nextAction;
                    nextAction = null;

                    currentAction.Load();
                }
            }

            currentAction.Update(deltaTime);

            if (currentAction.IsFinished())
            {
                currentAction.Unload();
                currentAction = null;
            }
        }
    }

}
